home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / mgr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-17  |  13.3 KB  |  522 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: mgr.c,v 1.2 89/03/17 09:06:21 sau Exp $
  9.     $Source: /m1/mgr.new/src/RCS/mgr.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /m1/mgr.new/src/RCS/mgr.c,v $$Revision: 1.2 $";
  12.  
  13. /* main routine for MGR */
  14.  
  15. #include "bitmap.h"
  16. #include <stdio.h>
  17. #include <sys/signal.h>
  18. #ifdef sun
  19. #include <sys/time.h> 
  20. #endif
  21. #include <errno.h>
  22. #include "defs.h"
  23. #include "menu.h"
  24. #include "font.h"
  25. #include "event.h"
  26.  
  27. #ifndef Min
  28. #define Min(x,y)        ((x)>(y)?(y):(x))
  29. #endif
  30. #define POLL(poll)        (poll&mask ? &set_poll : (struct timeval *) 0)
  31.  
  32. #ifdef sun
  33. struct timeval set_poll = {
  34.    (long) 0, (long) POLL_INT
  35.    };                /* set select to poll */
  36. #endif
  37.  
  38. char *mouse_dev = MOUSE_DEV;        /* name of mouse device */
  39. static int bitmaptype = 1;            /* old, non portable bitmap format */
  40.  
  41. main(argc,argv)
  42. int argc;
  43. char **argv;
  44.    {
  45.    register WINDOW *win;        /* current window to update */
  46.    register int i;            /* counter */
  47.    register int count;            /* # chars read from shell */
  48.    int maxbuf = MAXBUF;            /* # chars processed per window */
  49.    int shellbuf = MAXSHELL;        /* # chars processed per shell */
  50.    int type=1;                /* state var. for parsing argv */
  51.    int reads;                /* masks, result of select */
  52.    int tty;                /* fd for controlling tty */
  53.  
  54.    unsigned char c;            /* reads from kbd go here */
  55.    char start_file[MAX_PATH];        /* name of startup file */
  56.    char *screen_dev = SCREEN_DEV;    /* name of frame buffer */
  57.    char *default_font = (char * )0;    /* default font */
  58.    char *rindex(), *getenv(), *ttyname();
  59.    char *term = getenv("TERM");        /* place to put terminal name */
  60. #ifdef SHRINK
  61.    BITMAP *prime;
  62. #endif
  63.  
  64.    SETMOUSEICON(&mouse_arrow);
  65.     timestamp();                            /* initialize the timestamp */
  66.  
  67.    /* process arguments */
  68.  
  69.    sprintf(start_file,"%s/%s",getenv("HOME"),STARTFILE);
  70.    while(--argc > 0) {
  71.       argv++;
  72. #ifdef DEBUG
  73.       dprintf(S)(stderr,"argument %s type %c\r\n",*argv,type==1?'*':type);
  74. #endif
  75.       switch(type) {
  76.          case 1:    /* looking for flag */
  77.               if (**argv == '-')
  78.                  switch(type = *(*argv+1)) {
  79. #ifdef DEBUG
  80.                  case 'd': debug = 1;
  81.                            strcpy(debug_level,*argv+2);
  82.                            fprintf(stderr,"Debug level: [%s]\n",debug_level);
  83.                            type = 1;
  84.                            break;
  85. #endif
  86.                  case 'v':         /* print version number */
  87.                            printf("Mgr version %s created on %s at: %s\n",
  88.                                   version[0],version[1],version[2]);
  89.                            exit(1);
  90.                  case 'V':         /* print version and options */
  91.                            printf("Mgr version %s created on %s at: %s by %s\n",
  92.                                   version[0],version[1],version[2],version[5]);
  93.                            printf("Compile flags: %s\n",version[3]);
  94.                            printf("Home directory: %s\n",version[4]);
  95.                                     printf("built with: %s\n",version[6]);
  96.                            exit(1);
  97.                  case 'x': strcpy(start_file,"/dev/null");
  98.                            type = 1;
  99.                            break;
  100.                  case 'n':     /* use new style bitmap headers */
  101.                            bitmaptype = 0;
  102.                            break;
  103.                  }
  104.               else fprintf(stderr,"Invalid argument %s, ignored\n",*argv);
  105.               break;
  106.          case 'm':    /* set mouse device */
  107.               mouse_dev = *argv;
  108.               type = 1;
  109.               break;
  110.          case 's':    /* set start file */
  111.               strcpy(start_file,*argv);
  112.               type = 1;
  113.               break;
  114.          case 'F':    /* set default font file */
  115.               default_font = *argv;
  116.               type = 1;
  117.          case 'P':    /* set polling timeout */
  118.               set_poll.tv_usec = (long) atoi(*argv);
  119.               break;
  120.          case 'b':    /* set shell buffering */
  121.               shellbuf = atoi(*argv);
  122.               shellbuf = BETWEEN(5,shellbuf,1024);
  123.               break;
  124.          case 'B':    /* set window buffering */
  125.               maxbuf = atoi(*argv);
  126.               maxbuf = BETWEEN(1,maxbuf,shellbuf);
  127.               break;
  128.          case 'f':    /* set font directory */
  129.               font_dir = *argv;
  130.               type = 1;
  131.               break;
  132.          case 'i':    /* set icon directory */
  133.               icon_dir = *argv;
  134.               type = 1;
  135.               break;
  136.          case 'S':    /* set alternate frame buffer */
  137.               screen_dev = *argv;
  138.               type = 1;
  139.               break;
  140.          default:    /* invalid flag */
  141.              fprintf(stderr,"Invalid flag %c, ignored\r\n",type);
  142.               type = 1;
  143.               break;
  144.          }
  145.       }
  146.  
  147.    /* keep mgr from being run within itself */
  148.  
  149.    if (term && strcmp(TERMNAME,term)==0) {
  150.       fprintf(stderr,"Can't invoke mgr from within itself\n");
  151.       exit(1);
  152.       }
  153.    /* save tty modes for ptty's */
  154.  
  155.    save_modes(0);
  156.  
  157.    /* free all unused fd's */
  158.  
  159.    count = getdtablesize();
  160.    for(i=3;i<count;i++)
  161.       close(i);
  162.  
  163. #ifdef WHO
  164.    save_utmp(ttyname(0));
  165. #endif
  166.  
  167.    /* initialize the keyboard; sometimes a special device */
  168.    initkbd();
  169.  
  170.    /* initialize the bell; sometimes a special device requiring funnys */
  171.    initbell();
  172.  
  173.    /* get the default font file */
  174.  
  175.    if (default_font || (default_font = getenv(DEFAULT_FONT)))
  176.       font = open_font(default_font);
  177.  
  178.    if (font == (struct font *) 0)
  179.       font = open_font("");
  180.    font->ident = 0;
  181.  
  182.    /* set up the default font names */
  183.  
  184.    /* open the mouse */
  185.  
  186.    if ((mouse=open(mouse_dev,2)) <0) {
  187.       perror("can't find the mouse, or it is already in use\n");
  188.       exit(1);
  189.       }
  190.  
  191.    if (set_mouseio(mouse) < 0)
  192.       fprintf(stderr,"can't set mouse to exclusive use\n");
  193.  
  194.    mousex=mousey=32;
  195.  
  196.    /* find the screen */
  197.  
  198. #ifdef SHRINK            /* for sdh */
  199.  
  200. #ifndef X0
  201. # define X0 64
  202. #endif
  203. #ifndef Y0
  204. # define Y0 64
  205. #endif
  206. #ifndef W0
  207. # define W0 1024
  208. #endif
  209. #ifndef H0
  210. # define H0 772
  211. #endif
  212.  
  213.    if ((prime = bit_open(screen_dev)) == (BITMAP *) 0) {
  214.       perror("can't find the screen");
  215.       exit(2);
  216.       }
  217.    screen = bit_create(prime,X0&~0xf,Y0&~0xf,W0&~0xf,H0&~0xf);
  218. #else
  219.    if ((screen = bit_open(screen_dev)) == (BITMAP *) 0) {
  220.       perror("can't find the screen");
  221.       exit(2);
  222.       }
  223. #endif
  224.  
  225.    copyright(screen);
  226.  
  227.    set_tty(0);
  228.  
  229.    SETMOUSEICON(&mouse_cup);
  230.  
  231.    /* catch the right interrupts */
  232.  
  233.    for(i=0;i<NSIG;i++) switch(i) {
  234.       case SIGCHLD:     signal(SIGCHLD,sig_child);
  235.                         break;
  236.       case SIGILL:    /* <= 3.0 abort gererates this one */
  237.       case SIGCONT:
  238.       case SIGIOT:    /* 3.2 abort generates this (gee thanks, SUN!) */
  239.       case SIGQUIT:
  240.                         break;
  241.       case SIGTTIN:
  242.       case SIGTTOU:     signal(i,SIG_IGN);
  243.                         break;
  244.       default:          signal(i,catch);
  245.                         break;
  246.       }
  247.  
  248.    /* set the terminal type */
  249.  
  250.    if (term && strlen(term) >= strlen(TERMNAME))
  251.       strcpy(term,TERMNAME);
  252.  
  253.     /* get default font definitions */
  254.  
  255.       {
  256.       char buff[MAX_PATH];
  257.       sprintf(buff,"%s/%s",font_dir,STARTFILE);
  258.       startup(buff);
  259.       }
  260.  
  261.    /* process startup file */
  262.  
  263.    startup(start_file);
  264.    if (active != (WINDOW *) 0)
  265.       ACTIVE_ON();
  266.    else {
  267.       MOUSE_OFF(mousex,mousey);
  268.       erase_win(screen,0,0);
  269.       MOUSE_ON(mousex,mousey);
  270.    }
  271.  
  272.    /* turn on mouse cursor */
  273.  
  274.    MOUSE_OFF(mousex,mousey);
  275.    SETMOUSEICON(&mouse_arrow);
  276.    MOUSE_ON(mousex,mousey);
  277.  
  278.    /* void tty association         (does bad things when mgr crashes) */
  279.  
  280.    /*  void_tty();       Flakey ... Commented Out! */
  281.  
  282.    /* always look for keyboard and mouse input */
  283.  
  284.    mask |= (1<<mouse) | (1<<0); 
  285.  
  286.    /* main polling loop */
  287.  
  288.    while(1) {
  289.  
  290.       /* see if any window died */
  291.  
  292.       for(win=active;win != (WINDOW *) 0;) 
  293.          if (W(flags)&W_DIED) {
  294. #ifdef DEBUG
  295.             dprintf(d)(stderr,"Destroying %s-%d\r\n",W(tty),W(num));
  296. #endif
  297.             destroy(win);
  298.             win = active;
  299.             }
  300.          else
  301.             win = W(next);
  302.  
  303.       /* wait for input */
  304.  
  305.       reads = mask & ~poll;
  306.  
  307. #ifdef DEBUG
  308.       dprintf(l)(stderr,"select: mask=0x%x poll=0x%x 0x%x got\r\n",
  309.                  mask,poll,reads);
  310. #endif
  311. #ifdef sun
  312.       if (select(32,&reads,0,0,POLL(poll)) <0) {
  313. #ifdef DEBUG
  314.          dprintf(l)(stderr,"select failed %d->%d\r\n",
  315.                  reads, mask & ~poll);
  316.          if (debug) 
  317.             perror("Select:");
  318. #endif
  319.          reads = mask & ~poll;
  320.          continue;
  321.          }
  322. #else
  323.     ????? /* I need to package up select some how */
  324. #endif
  325. #ifdef DEBUG
  326.       dprintf(l)(stderr,"0x%x\r\n",reads);
  327. #endif
  328.  
  329.       /* process mouse */
  330.  
  331.       if (reads & (1<<mouse))
  332.          do {
  333.             proc_mouse(mouse);
  334.             }
  335.          while(mouse_count());
  336.       
  337.       /* process keyboard input */
  338.       
  339.       if (reads&1 && active && !(ACTIVE(flags)&W_NOINPUT)) {
  340.          read(0,&c,1);
  341. #ifdef BUCKEY
  342.          if ( (ACTIVE(flags)&W_NOBUCKEY)  ||  !do_buckey(c) )
  343.             write(ACTIVE(to_fd),&c,1);
  344. #else
  345.          write(ACTIVE(to_fd),&c,1);
  346. #endif
  347.             if (ACTIVE(flags)&W_DUPKEY && c==ACTIVE(dup))
  348.              write(ACTIVE(to_fd),&c,1);
  349.          continue;
  350.          }
  351.        else if (reads&1 && !active) {        /* toss the input */
  352.          read(0,&c,1);
  353. #ifdef BUCKEY
  354.          do_buckey(c);
  355. #endif
  356.      }
  357.  
  358.       /* process shell output */
  359.  
  360.       for(win=active;win != (WINDOW *) 0;win=W(next)) {
  361.          register int fd_bit = W(from_fd) ? 1<<W(from_fd) : 0;
  362.  
  363.          /* read data into buffer */
  364.  
  365.          if (fd_bit&reads&(~poll)) {
  366.             W(current) = 0;
  367.             if ((W(max) = read(W(from_fd),W(buff),shellbuf)) > 0) {
  368.                poll |= fd_bit;
  369. #ifdef DEBUG
  370.                dprintf(p)(stderr,"%s: reading %d [%.*s]\r\n",W(tty),
  371.                      W(max),W(max),W(buff));
  372. #endif
  373.                }
  374.             else {
  375.                poll &= ~fd_bit;
  376. #ifdef KILL
  377.                if (W(flags)&W_NOKILL)
  378.                   W(flags) |= W_DIED;
  379. #endif
  380. #ifdef DEBUG
  381.                if(debug) {
  382.                   fprintf(stderr,"%s: Select boo-boo fd(%d) code %d\r\n",
  383.                           W(tty),W(from_fd),W(max));
  384.                   perror(W(tty)); 
  385.                   }
  386. #endif
  387.                }
  388.             }
  389.  
  390.          /* check for window to auto-expose */
  391.  
  392.          if (fd_bit&poll && W(flags)&W_EXPOSE && !(W(flags)&W_ACTIVE)) {
  393. #ifdef DEBUG
  394.             dprintf(m)(stderr,"%s: activating self\r\n",W(tty));
  395. #endif
  396.             MOUSE_OFF(mousex,mousey);
  397.             cursor_off();
  398.             ACTIVE_OFF();
  399.             expose(win);
  400.             ACTIVE_ON();
  401.             cursor_on();
  402.             MOUSE_ON(mousex,mousey);
  403.             }
  404.  
  405.          /* write data into the window */
  406.  
  407.          if (fd_bit&poll && W(flags)&(W_ACTIVE|W_BACKGROUND)) {
  408.             
  409. #ifdef PRIORITY            /* use priority scheduling */
  410.             if (win==active)
  411.                count = Min(maxbuf,W(max)-W(current));
  412.             else if (W(flags)&W_ACTIVE)
  413.                count = Min(maxbuf>>1,W(max)-W(current));
  414.             else
  415.                count = Min(maxbuf>>2,W(max)-W(current));
  416. #else                /* use round robin scheduling */
  417.             count = Min(maxbuf,W(max)-W(current));
  418. #endif
  419.  
  420.             i = put_window(win,W(buff)+W(current),count);
  421. #ifdef DEBUG
  422.             dprintf(w)(stderr,"%s: writing %d/%d %.*s [%.*s]\r\n",
  423.                        W(tty),i,count,i,W(buff)+W(current),count-i,
  424.                        W(buff)+W(current)+i);
  425. #endif
  426.  
  427.             W(current) += i;
  428.             if (W(current) >= W(max))
  429.                poll &= ~fd_bit;
  430.             }
  431.          }
  432.       }
  433.    }
  434.  
  435. /*************************************************************************
  436.  *    catch dead children 
  437.  */
  438.  
  439. sig_child(sig)
  440. int sig;
  441.    {
  442.    register WINDOW *win;
  443.    extern int errno;
  444.  
  445.    /* see if a shell has died, mark deleted */
  446.  
  447. #ifdef DEBUG
  448.    dprintf(d)(stderr,"Looking for dead windows\r\n");
  449. #endif
  450.  
  451.    for(win=active;win != (WINDOW *) 0;win=W(next)) {
  452.       if (kill(W(pid),0) != 0 && !(W(flags)&W_NOKILL)) {
  453.          W(flags) |= W_DIED;
  454. #ifdef DEBUG
  455.          dprintf(d)(stderr, "%s-%d DIED\r\n",W(tty),W(num));
  456. #endif
  457.          }
  458.       }
  459.    }
  460.  
  461. /* process mouse */
  462.  
  463. int
  464. proc_mouse(mouse)
  465. int mouse;
  466.    {
  467.    int dx, dy;
  468.    static int still_down = 0;
  469.    register int button, done = 0;
  470.  
  471.    do {
  472.       button = mouse_get(mouse,&dx,&dy);
  473.       MOUSE_OFF(mousex,mousey);
  474.       mousex += 2*dx;
  475.       mousey -= 2*dy;
  476.       mousex = BETWEEN(0,mousex,BIT_WIDE(screen)-1);
  477.       mousey = BETWEEN(0,mousey,BIT_HIGH(screen)-1);
  478.       if (button != button_state) {
  479.          do_button( button );
  480.          done++;
  481.          }
  482.       MOUSE_ON(mousex,mousey);
  483.       } while (mouse_count() && !done);
  484.    return(done);
  485.    }
  486.  
  487. /* reopen the mouse after suspend */
  488.       
  489. int
  490. mouse_reopen()
  491.    {
  492.    int m = open(mouse_dev,2);
  493.    set_mouseio(m);
  494.    return(m);
  495.    }
  496.  
  497. /* return bitmap type */
  498.  
  499. int
  500. get_bm_type()
  501.    {
  502.    return (bitmaptype);
  503.    }
  504. /* return time stamp (100th's of seconds since startup) */
  505.  
  506. int
  507. timestamp()
  508.     {
  509.     static long offset = 0;
  510.     struct timeval timeval;
  511.     
  512.     gettimeofday(&timeval,0l);
  513.     
  514.     if (offset == 0) {
  515.         offset =  timeval.tv_sec;
  516.         return(0);
  517.         }
  518.     else {
  519.         return((timeval.tv_sec - offset) * 100 + timeval.tv_usec / 10000);
  520.         }
  521.     }
  522.